package com.xzlf;

import com.xzlf.netty.Client;
import com.xzlf.netty.TankJoinMsg;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import java.util.List;

public class TankFrame extends Frame {

    public static final TankFrame INSTANCE = new TankFrame();

    public static final int GAME_WIDTH = 1000, GAME_HEIGHT = 800;

    public Tank myTank = new Tank(200, 400, Dir.DOWN, true, Group.GOOD, this);
    public Map<UUID, Tank> tankMap = new HashMap<>();
    List<Explode> explodes = new ArrayList<>();

    List<Tank> tanks = new ArrayList<>();

    List<Bullet> bullets = new ArrayList<>();

    Image buf;

    Client client;

    private TankFrame() throws HeadlessException {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setLocation(500, 500);
        setTitle("坦克大战");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        addKeyListener(new MyKeyListener());
        TankJoinMsg msg = new TankJoinMsg(myTank);
        this.client = new Client(msg);
        setVisible(true);
    }


    public void addTank(Tank tank) {
        tankMap.put(tank.getId(), tank);
    }

    @Override
    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.blue);
        g.setFont(new Font("雅黑", Font.BOLD, 30));
        g.drawString("子弹数量：" + bullets.size(), 20, 100);
        g.drawString("敌方坦克数量：" + tanks.size(), 20, 150);
        g.setColor(color);
//        myTank.paint(g);

        if (tankMap.size() > 0) {
            tankMap.values().stream().forEach(e -> e.paint(g));
        }

        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }

        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }
        // 碰撞检测
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).colid(tanks.get(j));
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (buf == null) {
            buf = createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics graphics = buf.getGraphics();
        Color color = graphics.getColor();
        graphics.setColor(Color.black);
        graphics.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        graphics.setColor(color);
        paint(graphics);
        g.drawImage(buf, 0, 0, null);
    }

    class MyKeyListener extends KeyAdapter {
        boolean bu, bd, br, bl;

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_W:
                    bu = true;
                    break;
                case KeyEvent.VK_S:
                    bd = true;
                    break;
                case KeyEvent.VK_A:
                    bl = true;
                    break;
                case KeyEvent.VK_D:
                    br = true;
                    break;
                default:
                    break;

            }
            setMainTankDir();

        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_W:
                    bu = false;
                    break;
                case KeyEvent.VK_S:
                    bd = false;
                    break;
                case KeyEvent.VK_A:
                    bl = false;
                    break;
                case KeyEvent.VK_D:
                    br = false;
                    break;
                case KeyEvent.VK_J:
                    myTank.fire();
                    break;
                default:
                    break;

            }
            setMainTankDir();
        }

        private void setMainTankDir() {
            if (!bu && !bd && !br && !bl)
                myTank.setMoving(false);
            else {
                myTank.setMoving(true);
                if (bu) myTank.setDir(Dir.UP);
                if (bd) myTank.setDir(Dir.DOWN);
                if (br) myTank.setDir(Dir.RIGHT);
                if (bl) myTank.setDir(Dir.LEFT);
            }
        }
    }


}
